% interpolation
close all;
clc;

scale_change = 4;
sigma_filter= 1;
filter_size= 5;
delta = 1;

Ytrue = imread('mandrill_lumi.bmp');
filter = fspecial('gaussian', filter_size, sigma_filter);

%on applique un filtre passe bas
X = imfilter(Ytrue,filter,'symmetric','same');

%on creer notre image basse resolution
X = imresize(X, 1/scale_change, 'bicubic');

%on creer notre estime de notre image haute resolution par interpolation
Y = imresize(X, scale_change, 'bicubic');

psnr(Ytrue,Y)

subplot(1,3,1);
imshow(Ytrue);
title('Image Originale');

subplot(1,3,2);
imshow(Y);
title('Interpolation bicubic');

for i=1:30,
    Ydiminue = imfilter(Y,filter,'symmetric','same');
    Ydiminue = imresize(Ydiminue, 1/scale_change, 'bicubic');
    
    erreur_lr = X - Ydiminue;
    erreur_hr = imresize(erreur_lr, scale_change, 'bicubic');
       
    %Y = imresize(Ydiminue, scale_change, 'bicubic');
    Y = Y + (erreur_hr * delta);
   
    %[psnr,mse,maxerr,l2rat] = measerr(Ytrue,Y);
    %psnr
    
    
end

psnr(Ytrue,Y)

subplot(1,3,3);
imshow(Y);
title('Image back-projection');

Ytrue_fft = fftshift(fft2(Ytrue));
Y_fft = fftshift(fft2(Y));

figure;

subplot(1,2,1);
imagesc(log(1+abs(Ytrue_fft)));
title('Spectre image originale');

subplot(1,2,2);
imagesc(log(1+abs(Y_fft)));
title('Spectre image back-projection');

